var dg_role_accredit = $('#dg_role_accredit');
var dg_user_accredit = $('#dg_user_accredit');
var dg_user_accredit_reverse = $('#dg_user_accredit_reverse');
var dg_menu_accredit = $('#dg_menu_accredit');
var currentRoleId = null;
$(function () {
    dg_role_accredit.datagrid({
                                  url: ctx + '/sys/role/search',
                                  method: 'get',
                                  onSelect: function (index,row) {
                                      currentRoleId = row.id;
                                      dg_user_accredit.datagrid({
                                                                    url:ctx+'/sys/user/search?roleId='+currentRoleId,
                                                                    method:'get'
                                                                })
                                      dg_user_accredit_reverse.datagrid({
                                                                            url:ctx+'/sys/user/search?notJoinRoleId='+currentRoleId,
                                                                            method:'get'
                                                                        })
                                      dg_menu_accredit.treegrid({
                                                                    url:ctx+'/sys/menu/tree_data?roleId='+currentRoleId,
                                                                    method:'get',
                                                                    idField: 'id',
                                                                    treeField: 'name'
                                                                })
                                  }
                              });

})


//删除用户关联
var opt_role_user =  function(value, row, index){
    var str = '<a href="javascript:void(0);" style="color: red" onclick="roleUserRemove('+value+')">删除</a>';
    return str;
}

var roleUserRemove = function(userId){
    subMitUserRoleLimit(false,currentRoleId,[userId]);
}
//删除菜单关联
var opt_role_menu =  function(value, row, index){
    var str = '<a href="javascript:void(0);" style="color: red" onclick="roleMenuRemove('+value+')">删除</a>';
    return str;
}

var roleMenuRemove = function(menuId){
    //TODO 调用后台删除
}

//打开关联用户的弹框
var accredit_user_open = function(){
    var selected = dg_role_accredit.datagrid("getSelected");
    if (!selected) {//如果被选中的不存在，给出提示，结束程序
        $.messager.alert('系统提示', "请先选择一个您要操作的角色");
        return;
    }
    $('#dl_user_accredit_reverse').dialog('open');
}

var role_user_limit_add = function(){
    var checked = dg_user_accredit_reverse.datagrid('getChecked');
    if(!checked){
        $.messager.alert('系统提示', "请选择您要关联的用户");
        return;
    }

    var userIds = [];
    checked.forEach(function(item,index){
        userIds.push(item.id);
    });
    subMitUserRoleLimit(true,currentRoleId,userIds);
}

var subMitUserRoleLimit = function(checked,roleId,userIds){
    $.ajax({
               url: ctx + 'sys/limit/user_role',
               contentType: 'application/json',
               dataType: 'json',
               data: JSON.stringify({checked: checked, userIds: userIds, roleId: currentRoleId}),
               type: "put",
               success: function (result) {
                   dg_user_accredit.datagrid('reload');
                   dg_user_accredit_reverse.datagrid('reload');
                   $('#dl_user_accredit_reverse').dialog('close');
               },
               error: function () {
                   $.messager.alert('系统提示', "服务器连接出错");
               }
           });
}